package com.signway.swmediaplayerdemo;

import static com.signway.swmediaplayerdemo.MainActivity.hideBottomUIMenu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.signway.swmediaplayer.SWMediaPlayer;
import com.signway.swmediaplayer.utils.SystemProperties;
import com.signway.swmediaplayerdemo.databinding.ActivityListSwitchBinding;

public class ListSwitchActivity extends AppCompatActivity {
    private static final String TAG = ListSwitchActivity.class.getName();
    private ActivityListSwitchBinding binding;
    private SurfaceView mSurfaceView;

    private SWMediaPlayer mSWMediaPlayer;

    private boolean is2K = false;
//    private final String VideoPath2K = Environment.getExternalStorageDirectory().getPath() + "/testmp5/2_v_25_1080.mp4";
    private final String VideoPath2K = Environment.getExternalStorageDirectory().getPath() + "/testmp4/2_v_25.mp4";
    private final String VideoPath4K = Environment.getExternalStorageDirectory().getPath() + "/testmp4/2_v_25.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityListSwitchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        hideBottomUIMenu(this);

        mSurfaceView = new SurfaceView(this);
        binding.surfaceLayout.addView(mSurfaceView);
        mSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
        mSurfaceView.getHolder().addCallback(new SurfaceCallBack(this, mSWMediaPlayer));
        mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mSWMediaPlayer = new SWMediaPlayer();
        mSWMediaPlayer.setOnPreparedListener(mOnPreparedListener);
        mSWMediaPlayer.setOnInfoListener(mOnInfoListener);
        mSWMediaPlayer.setOnCompletionListener(mOnCompletionListener);

        binding.init.setOnClickListener(v -> {
            switchMedia();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSWMediaPlayer.release();
    }

    private void switchMedia() {
        if (is2K) {
            initPlayer2K();
            is2K = false;
        } else {
            initPlayer4K();
            is2K = true;
        }
        binding.init.setVisibility(View.GONE);
    }

    private SWMediaPlayer.OnCompletionListener mOnCompletionListener =
            (mp, lastRenderTimestampNs) -> SWMediaPlayer.MEDIA_COMPLETION_STOP;
    private SWMediaPlayer.OnPreparedListener mOnPreparedListener =
            mp -> mp.start();
    private SWMediaPlayer.OnInfoListener mOnInfoListener = (mp, what, extra) -> {
        switch (what) {
            case SWMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
//                SystemProperties.setProp("vendor.mstar.mmvideo.present", "true");
                break;
            case SWMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_END:
//                SystemProperties.setProp("vendor.mstar.mmvideo.present", "false");
                binding.init.setVisibility(View.VISIBLE);
                switchMedia();
                break;
        }
        return true;
    };

    private void initPlayer4K() {
        mSurfaceView.setLayoutParams(new RelativeLayout.LayoutParams(2160, 3840));

        if (mSWMediaPlayer != null) {
            try {
                mSWMediaPlayer.reset();
                mSWMediaPlayer.setDataSource(VideoPath4K);
                mSWMediaPlayer.setDisplay(mSurfaceView.getHolder());
                mSWMediaPlayer.prepare();
                Log.d(TAG,  "mPlayer.getDuration() " + mSWMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
                mSWMediaPlayer.release();
                Toast.makeText(this, "无法播放", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void initPlayer2K() {
        mSurfaceView.setLayoutParams(new RelativeLayout.LayoutParams(1080, 1920));

        if (mSWMediaPlayer != null) {
            try {
                mSWMediaPlayer.reset();
                mSWMediaPlayer.setDataSource(VideoPath2K);
                mSWMediaPlayer.setDisplay(mSurfaceView.getHolder());
                mSWMediaPlayer.prepare();
                Log.d(TAG,  "mPlayer.getDuration() " + mSWMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
                mSWMediaPlayer.release();
                Toast.makeText(this, "无法播放", Toast.LENGTH_LONG).show();
            }
        }
    }

    private class SurfaceCallBack implements SurfaceHolder.Callback {

        public SurfaceCallBack(Context context, SWMediaPlayer player) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated " + holder.toString());
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "surfaceChanged width " + width + ", height" + height);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");
        }
    }
}